package com.zheng.PointTest.controller;

import com.zheng.PointTest.base.response.BaseResponse;
import com.zheng.PointTest.base.response.ResponseResult;
import com.zheng.PointTest.entity.home.IndexInfo;
import com.zheng.PointTest.entity.home.hospital;
import com.zheng.PointTest.entity.home.nav2;
import com.zheng.PointTest.entity.home.slide;
import com.zheng.PointTest.mapper.HospitalHomeMapper;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * 类描述:[HospitalHomeInfoController]
 * 说明:处理医院首页信息的访问处理
 *
 * @author: zjl
 * @Date: 2025/5/22 10:33
 */
@BaseResponse
@RestController
@RequestMapping("index")
public class HospitalHomeInfoController {

    @Resource
    HospitalHomeMapper hospitalHomeMapper;
    // 假设图片存储在服务器的某个目录（例如: /uploads/images/）
//    private  static final String IMAGE_DIR = "/uploads/images/";
    private static final String IMAGE_DIR = "C:\\Users\\uniriho\\Desktop\\hospitalImage\\";

    @GetMapping("/home")
    public ResponseResult getHomeData() {
        String imagePath = "http://192.168.99.74:8001/images/image1.jpg";
        String pic_image_url = "http://192.168.99.74:8001/images/image";

        //查询医院信息
        List<hospital> hospitals = hospitalHomeMapper.queryAllHospital();
        hospitals.forEach(hospital ->
                hospital.setAvatar_url(imagePath));

        IndexInfo indexInfo = new IndexInfo();
        List<slide> slides = new ArrayList<>();
        for (int i = 1; i < 4; i++) {
            slide slide = new slide();
            slide.setPic_image_url(pic_image_url + i + ".jpg");
            slides.add(slide);
        }
        indexInfo.setSlides(slides);
        List<nav2> nav2 = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            nav2 nav21 = new nav2();
            nav21.setPic_image_url(pic_image_url + i + ".jpg");
            nav2.add(nav21);
        }
        indexInfo.setNav2s(nav2);
        indexInfo.setHospitals(hospitals);
        return new ResponseResult(0, "success", indexInfo);
    }

    // 配置静态资源映射，使图片可通过URL直接访问
    @Configuration
    public class WebConfig implements WebMvcConfigurer {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/images/**")
                    .addResourceLocations("file:" + IMAGE_DIR);
        }
    }
}
